package com.shop.manager.controller;

//import com.shop.manager.entity.Seller;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Seller;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.SellerService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 商家增删改查
 */
@RestController
@RequestMapping("/seller")
public class SellerController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private SellerService sellerService;

    /**
     * @Description 添加商家
     * @param seller 商家对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Seller 对象") Seller seller) {
        try {
            sellerService.add(seller);
            return HttpResult.ok("添加商家成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加商家失败!");
    }

    /**
     * @Description 批量删除商家
     * @param sellerIds 商家id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody(required = false) String[] sellerIds) {
        try {
            sellerService.deleteByIds(sellerIds);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改商家
     * @param seller 修改的商家对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Seller 对象") Seller seller) {
        try {
            sellerService.update(seller);
            return HttpResult.ok("修改商家成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改商家失败!");
    }

    /**
     * @Description 根据ID主键查询商家
     * @param id 商家id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") String id) {
        return HttpResult.ok(sellerService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param seller 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "Seller 对象") Seller seller){
        try {
            //分页查询
            PageResult result = sellerService.findPage(pageNum, size, seller);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询商家异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = sellerService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询商家
     * @param seller 商家的对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false)  @ApiParam(value = "Seller 对象") Seller seller) {
        return HttpResult.ok(sellerService.findByWhere(seller));
    }

    /**
     * @Description 修改商家状态
     * @param sellerId 商家id
     * @param status 商家状态
     * @return
     */
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public HttpResult updateStatus(@RequestParam("sellerId") String sellerId, @RequestParam("status") String status) {
        int mcount = sellerService.updateStatus(sellerId, status);
        if (mcount > 0) {
            return HttpResult.ok("审核成功!");
        }
        return HttpResult.error("审核失败!");
    }

}
